package com.glb.gilibuy.service.impl;

import com.github.pagehelper.PageInfo;
import com.glb.gilibuy.dao.ProductDao;
import com.glb.gilibuy.entity.*;
import com.glb.gilibuy.service.ProductCategoryService;
import com.glb.gilibuy.service.ProductService;
import com.glb.gilibuy.util.BeanUtil;
import com.glb.gilibuy.util.SnowflakeIdUtil;
import com.glb.gilibuy.web.vo.ProductCategoryVO;
import com.glb.gilibuy.web.vo.SearchedProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/3 14:21
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductCategoryService productCategoryService;
//
    @Override
    public Product selectProductByProductId(String productId) {
        if (!"".equals(productId)) {
            ProductExample productExample = new ProductExample();
            productExample.or().andProductIdEqualTo(productId).andIsDeletedEqualTo(0);
            List<Product> productList = productDao.selectByExample(productExample);
            if (productList.isEmpty()) {
                return null;
            }
            return productList.get(0);
        }
        return null;
    }

    @Override
    public List<Product> selectProductsByCategoryId(String categoryId, ProductExample orderBy, Integer greaterThan) {
        if (!"".equals(categoryId)) {
            orderBy.or().andProductCategoryIdEqualTo(categoryId).andProductAmountGreaterThan(greaterThan).andIsDeletedEqualTo(0);
            List<Product> productList = productDao.selectByExample(orderBy);
            return productList;
        }
        return null;
    }

    @Override
    public List<Product> selectProductsByCategoryIds(String[] categoryIds, ProductExample orderBy, Integer greaterThan) {
        for (String categoryId : categoryIds) {
            if ("".equals(categoryId)) {
                continue;
            }
            orderBy.or().andProductCategoryIdEqualTo(categoryId).andProductAmountGreaterThan(greaterThan).andIsDeletedEqualTo(0);
        }
        List<Product> productList = productDao.selectByExample(orderBy);
        return productList;
    }

    @Override
    public List<Product> selectProductsByKeyword(String keyword, ProductExample orderBy, Integer greaterThan) {
        if (!"".equals(keyword)) {
            orderBy.or().andProductNameLike("%" + keyword + "%").andProductAmountGreaterThan(greaterThan).andIsDeletedEqualTo(0);
        } else {
            orderBy.or().andProductAmountGreaterThan(greaterThan).andIsDeletedEqualTo(0);
        }
        List<Product> productList = productDao.selectByExample(orderBy);
        return productList;
    }

    @Override
    public PageInfo findProductsByCategoryIds(String[] categoryIds, ProductExample orderBy, Integer greaterThan) {

        List<Product> productList = selectProductsByCategoryIds(categoryIds, orderBy, greaterThan);
        if (productList.isEmpty()) {
            return null;
        }
        PageInfo pageInfo = new PageInfo(productList);
        List<SearchedProductVO> productVOList = BeanUtil.copyList(productList, SearchedProductVO.class);

        for (SearchedProductVO searchedProductVO : productVOList) {
            ProductCategory productCategory = productCategoryService.selectCategoryByCategoryId(searchedProductVO.getProductCategoryId());

            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            BeanUtil.copyOne(productCategory, productCategoryVO);
            searchedProductVO.setProductCategory(productCategoryVO);
        }
        pageInfo.setList(productVOList);

        return pageInfo;
    }

    @Override
    public int addProduct(Product product) {
        if (product == null) {
            return 0;
        }

        //后台不校参

        if (productDao.insertSelective(product) > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateProduct(Product product) {
        if (product == null) {
            return 0;
        }

        //后台不校参

        if (productDao.updateByPrimaryKeySelective(product) > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<Product> findAllProduct() {
        ProductExample example = new ProductExample();
        example.or();
        if (productDao.selectByExample(example).isEmpty()) {
            return null;
        }
        List<Product> products = productDao.selectByExample(example);
        return products;
    }
}
